<template>
  <el-drawer
    v-model="dialogFormVisible"
    :title="title"
    direction="rtl"
    size="40%"
    @close="close"
  >
    <el-form ref="formRef" :model="job" label-width="100px" :rules="rules">
      <el-form-item label="选择任务执行人">
        <el-select
          v-model="job.boyi_to_uid"
          clearable
          placeholder="请选择任务执行人"
        >
          <el-option
            v-for="(item1, index1) in userList"
            :key="index1"
            :label="item1.name"
            :value="item1.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务佣金">
        <el-input v-model="job.amount" type="number" />
      </el-form-item>
      <el-form-item label="任务备注">
        <el-input v-model="job.remark" type="textarea" />
      </el-form-item>
      <el-form-item label="截止日期">
        <el-input
          v-model="job.diedline"
          type="date"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD 23:59:59"
        />
      </el-form-item>
      <el-form-item label="任务附件">
        <el-upload
          v-model:file-list="job.file_list"
          class="upload-demo"
          multiple
          :action="baseURL + '/api/file/file'"
        >
          <el-button type="primary">点击选择文件</el-button>
          <template #tip>
            <div class="el-upload__tip">请上传文件附件，大小不超过10M</div>
          </template>
        </el-upload>
      </el-form-item>
      <div style="text-align: center">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-form>
  </el-drawer>
</template>

<script>
  import { doEdit } from '@/api/boyiJob'
  import { getUserList } from '@/api/user'
  import { baseURL } from '@/config'
  import { parseTime } from '@/utils'
  export default defineComponent({
    name: 'BoyiJobEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')
      const state = reactive({
        formRef: null,
        form: {
          roles: [],
        },
        userList: [],
        rules: {
          boyi_to_uid: [
            { required: true, trigger: 'blur', message: '必须选择任务执行人' },
          ],
        },
        title: '',
        job: {},
        baseURL: baseURL,
        dialogFormVisible: false,
      })

      const showEdit = (row) => {
        if (!row) {
          state.title = '添加'
        } else {
          state.title = '编辑'
          if (row.diedline) {
            row.diedline = parseTime(row.diedline, '{y}-{m}-{d}')
          }
          state.job = Object.assign({ form: {} }, row)
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        // state['formRef'].resetFields()
        state.form = {
          roles: [],
        }
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const file_id_list = []
            state.job.file_list.forEach((item) => {
              file_id_list.push(item.id || item.response.data.id)
            })
            state.job.file_id_list = file_id_list
            const { msg } = await doEdit(state.job)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      const getUserListData = async () => {
        const {
          data: { list },
        } = await getUserList()
        state.userList = list
      }

      onMounted(() => {
        getUserListData()
      })

      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
  })
</script>
